//
//  BKNewsDetailViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/17.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKNewsDetailViewController.h"
#import "BKNewViiewModel.h"
#import <UIImageView+AFNetworking.h>
#import <MBProgressHUD.h>
@interface BKNewsDetailViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeight;
@property (weak, nonatomic) IBOutlet UIScrollView *contentView;

@end

@implementation BKNewsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _titleLabel.text = _typeName;
    
    [self p_loadData];
    
}
- (IBAction)p_back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)p_loadData{

    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.contentView];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"正在加载";
    [self.view addSubview:hud];
    [hud removeFromSuperViewOnHide];
    [hud show:YES];
    _contentView.hidden = YES;
    __weak BKNewsDetailViewController *weakSelf = self;
    [BKNewViiewModel detailWithId:_articleId success:^(NSDictionary *dic) {
        [hud hide:YES];
        _contentView.hidden = NO;
        weakSelf.articleTitle.text = dic[@"title"];
        weakSelf.timeLabel.text  = dic[@"time"];
        
        if ([dic[@"img"] length]==0) {
            _imageViewHeight.constant = 0;
            
        }
        weakSelf.fromLabel.text = dic[@"origin"];
        weakSelf.timeLabel.text = dic[@"time"];
        
        [weakSelf.imageView setImageWithURL:[NSURL URLWithString:dic[@"img"]]];
        NSString *concent = dic[@"content"];
        weakSelf.contentLabel.text = concent;
        
        UIFont * tfont = [UIFont systemFontOfSize:14];
        
        
        CGSize size =CGSizeMake(_contentLabel.frame.size.width,1000);
      
        
        NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
        
        //ios7方法，获取文本需要的size，限制宽度
        
        CGSize  actualsize =[concent boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
        
        _height.constant = actualsize.height;
        
        
    } failure:^(NSString *message) {
        UIAlertView *alert =[ [UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alert show];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
